package com.example.hotelsystem.service.impl;

import code.CodeError;
import com.example.hotelsystem.entity.FloorDetails;
import com.example.hotelsystem.entity.RoomType;
import com.example.hotelsystem.mapper.FloorDetailsMapper;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.server.Server;
import com.example.hotelsystem.service.FloorService;
import com.example.hotelsystem.service.FloorDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@Service
public class FloorDetailsServiceImpl extends ServiceImpl<FloorDetailsMapper, FloorDetails> implements FloorDetailsService {
    @Autowired
    FloorService floorService;
    @Autowired
    FloorDetailsService FloorDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result upLoadImg(MultipartFile file){
        String fileName;
        fileName = file.getOriginalFilename();
        fileName= UUID.randomUUID()+"_"+fileName;
        String dirPath = "/www/server/tomcat/webapps/floor/image/";
        File filePath = new File(dirPath);
        if(!filePath.exists()){
            filePath.mkdirs();
        }
        try{
            file.transferTo((new File(dirPath+fileName)));
            String encoderUrl = URLEncoder.encode(fileName, "UTF-8");
            String fileurl= "http://" + Server.iphost+":"+ Server.port + "/floor/image/"+encoderUrl;
            return Result.success("操作成功",fileurl);

        }catch (Exception e){
            e.printStackTrace();
            return Result.error(CodeError.INTERNAL_EXCEPTION,"内部异常");
        }

    }

}
